// 元素的拖拽实现
var params = {
	left: 0,
	top: 0,
	currentX: 0,
	currentY: 0,
	flag: false
};

//拖拽的实现
var startDrag = function(outerobj, bar, target, callback){
	if(target.css("left")!=="auto"){
		params.left=target.css("left");
	}
	if(target.css("top")!=="auto"){
		params.top=target.css("top");
	}
	bar.on("mousedown",function(event){
		params.flag = true;
		if(!event){
			event = window.event;
			//防止IE文字选中
			bar.on("selectstart",function(){
				return false;
			});
		}
		var e = event;
		params.currentX = e.clientX;
		params.currentY = e.clientY;
	});
	$(document).on("mouseup",function(){
		params.flag = false;	
		if(target.css("left")!=="auto"){
			params.left=target.css("left");
		}
		if(target.css("top")!=="auto"){
			params.top=target.css("top");
		}
	});
	$(document).on("mousemove",function(event){
		var e = event ? event: window.event;
		if(params.flag){
			var nowX = e.clientX, nowY = e.clientY;
			var disX = nowX - params.currentX, disY = nowY - params.currentY;
			target.css("left",parseInt(params.left) + disX);
			target.css("top",parseInt(params.top) + disY);
			let top=parseInt(target.css("top")),
			    left=parseInt(target.css("left")),
			    right=parseInt(outerobj.css("width"))-parseInt(target.css("width"))-parseInt(target.css("left"))-2,
			    bottom=parseInt(outerobj.css("height"))-parseInt(target.css("top"))-parseInt(document.getElementById("loandetail-table").clientHeight)-parseInt(target.css("height"))-2;
			if(top<=0){
				target.css("top",0);
			}
			if(left<=0){
				target.css("left",0);
			}
			if(right<=0){
				target.css("left",parseInt(outerobj.css("width"))-parseInt(target.css("width")));
			}
			if(bottom<=0){
				target.css("top",parseInt(outerobj.css("height"))-parseInt(document.getElementById("loandetail-table").clientHeight)-parseInt(target.css("height")));
			}
			if (event.preventDefault) {
				event.preventDefault();
			}
			return false;
		}
		
		if (typeof callback == "function") {
			callback(parseInt(params.left) + disX, parseInt(params.top) + disY);
		}
	});
};

// mousedown：记录鼠标当前坐标
// mousemove：对比当前坐标与mousedown的坐标
// div宽
// 	width : div.height + mousedownX - mousemoveX
// 	heigth : div.height + mousedownY - mousemoveY
// mouseup之前重复前面的计算
// mouseup后停止执行mousemove事件

var paramsw = {
	currentX: 0,
	width:0,
	owidth:0,
	flag: false
};

var resizableW=function(bar,obox){
	bar.on("mousedown",function(){
		paramsw.flag=true;
		if(!event){
			event=window.event;
			// bar.onselectstart=function(){
			// 	return false;
			// }
			bar.on("selectstart",function(){
				return false;
			});
		}
		var e=event;
		paramsw.currentX=e.clientX;
		// paramsw.owidth=obox.clientWidth;
		paramsw.owidth=obox.width();
	});
	$(document).on("mouseup",function(event){
		paramsw.flag=false;
	});
	$(document).on("mousemove",function(event){
		var e = event ? event: window.event;
		if(paramsw.flag){
			var nowX = e.clientX ;
			var disX = nowX - paramsw.currentX;
			var x = parseInt(paramsw.owidth) + disX;
			// obox.style.width=x+"px";
			obox.css("width",x);
			if (event.preventDefault) {
				event.preventDefault();
			}
			return false;
		}
	});
}
var paramsh = {
	currentY: 0,
	height:0,
	oheight:0,
	flag: false
};
var resizableH=function(bar,obox){
	bar.on("mousedown",function(event){
		paramsh.flag=true;
		if(!event){
			event=window.event;
			bar.on("selectstart",function(){
				return false;
			});
		}
		var e=event;
		paramsh.currentY=e.clientY;
		// paramsh.oheight=obox.clientHeight;
		paramsh.oheight=obox.height();
	});
	$(document).on("mouseup",function(event){
		paramsh.flag=false;
	});
	$(document).on("mousemove",function(event){
		var e = event ? event: window.event;
		if(paramsh.flag){
			var nowY = e.clientY ;
			var disY = nowY - paramsh.currentY;
			var y = parseInt(paramsh.oheight) + disY;
			// obox.style.height=y+"px";
			obox.css("height",y);
			if (event.preventDefault) {
				event.preventDefault();
			}
			return false;
		}
	});
};
